var http = require("../../utils/http.js");
var baseConfig = require("../../baseConfig");
Page({
  data: {
    TabCur: 0,
    scrollLeft: 0,
    page: 0,
    curId: 1,
    list: [],
    shouyiList: [],
    userList: [{
      acatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Rbau05TrcUBU0BcIMxOZY6ibdUiagF4GdWvXNlAqq04pEt44AZjiaGwrYr92JXNtPfRuVBMoktyaEEib8PXoL2I4lg/132',
      nickname: '后来居上',
      totalRevenue: '144.00',
    }],
    teamTag: false,
    tabs: [{
      id: 1,
      name: "团队成员"
    }, {
      id: 2,
      name: "收益明细"
    }, ],
    layer2Popup: false,
  },
  onLoad: function (options) {
    this.getList()
    this.getDate()
    this.getUser()
  },
  copyWxNum2() { // 复制微信号2
    // let wxnum = wx.getStorageSync('tuserInfo').wxnum
    wx.setClipboardData({
      // data: wxnum,
      data: 'logic-yuan',
      success: function (res) {
        wx.showToast({
          title: "复制成功~",
          icon: "none"
        });
      },
      fail: function (err) {
        console.log(err);
      }
    });
  },
  saveImageToPhotos2(filePath) {
    let that = this
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success: () => {
              that.saveImage2(filePath)
              that.setData({
                isGetSet: true,
                layer1Popup: false
              })
              wx.showToast({
                title: '保存图片至相册',
                icon: 'success',
                duration: 2000
              })
            },
            fail: () => {
              that.setData({
                isGetSet: false
              })
            }
          })
        } else {
          that.setData({
            isGetSet: true,
            layer1Popup: false
          })
          that.saveImage2(filePath)
          wx.showToast({
            title: '保存图片至相册',
            icon: 'success',
            duration: 2000
          })
        }
      },
      fail: (res) => {
        console.log(res);
      }
    })
  },
  saveImage2(filePath) {
    wx.saveImageToPhotosAlbum({
      filePath: "/static/lisa.jpg",
      success: (res) => {
        // console.log(res)
        wx.showToast({
          title: '保存成功',
          icon: "none"
        });
      },
      fail: (err) => {
        console.log(err)
        wx.showToast({
          title: '保存失败',
          icon: "none"
        });
      }
    })
  },
  savePhotoToAlbum2(res) { // 官方客服-保存到相册
    this.saveImageToPhotos2(res)
  },
  customBtnClick() { // 官方客服弹窗
    this.setData({
      layer2Popup: true
    })
  },
  closeClick2() { // 关闭官方客服弹窗2
    this.setData({
      layer2Popup: false
    })
  },
  decideDate() { // 判断是否是同一天
    console.log(wx.getStorageSync('date'));
  },
  tabSelect(e) {
    var index = e.currentTarget.dataset.id
    this.setData({
      TabCur: index,
      list: [],
      shouyiList: [],
      page: 0
    })
    if (index == 0) {
      this.data.curId = 1
    } else {
      this.data.curId = 2
    }
    this.getList()
  },
  getShouyiList: function () {
    var that = this
    http.reqLoading("get_my_team_revenue", {
      p: this.data.page,
      f: 100
    }, res => {
      wx.stopPullDownRefresh()
      that.data.shouyiList = that.data.shouyiList.concat(res.data.content)
      that.setData({
        curId: 2,
        shouyiList: that.data.shouyiList
      })
    })
  },
  getTuanduiList: function () {
    var that = this
    http.reqLoading("get_my_team_list", {
      p: this.data.page,
      f: 100
    }, res => {
      // console.log("tuandui===", JSON.stringify(res))
      wx.stopPullDownRefresh()
      that.data.list = that.data.list.concat(res.data.content)
      that.setData({
        curId: 1,
        list: that.data.list
      })
    })
  },
  getList: function () {
    if (this.data.curId == 2) {
      this.getShouyiList()
    } else {
      this.getTuanduiList()
    }
  },
  onPullDownRefresh: function () {
    this.setData({
      page: 0,
      list: [],
      shouyiList: []
    })
    this.getList()
  },
  onReachBottom: function () {
    this.data.page = this.data.page + 1
    this.getList()
  },
  getDate() { // 获取前一天日年月日
    var today = new Date();
    var yesterday_milliseconds = today.getTime() - 1000 * 60 * 60 * 24;
    var yesterday = new Date();
    yesterday.setTime(yesterday_milliseconds);

    var strDay = yesterday.getDate();
    var strMonth = yesterday.getMonth() + 1;
    var Year = yesterday.getFullYear();
    var Month = strMonth < 10 ? "0" + strMonth : strMonth;
    var Day = strDay < 10 ? "0" + strDay : strDay;
    const strYesterday = Year + "-" + Month + "-" + Day;
    console.log(strYesterday);
    this.setData({
      yearMonthDay: strYesterday
    });
    // wx.setStorageSync('date', strYesterday);
  },
  getUser() {
    let uid = wx.getStorageSync('userid')
    wx.request({
      url: baseConfig.baseUrl + `get_user_tram_total_revenue`,
      method: 'GET',
      data: {
        uid: uid,
      },
      success: (res) => {
        // console.log(res.data.data);
        this.setData({
          totalRevenue: res.data.data.totalRevenue,
          avatarUrl: res.data.data.avatarUrl,
          nickName: res.data.data.nickName,
        })
        if (res.data.data.teamTag) {
          console.log('存在teamTag');
          this.setData({
            teamTag: true
          })
        } else {
          console.log('不存在teamTag:', this.data.teamTag);
        }
      }
    });
  }
})